<?php

namespace admin\models;

use ijony\helpers\File;
use ijony\helpers\Folder;
use ijony\helpers\Utils;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%products}}".
 *
 * @property string $id 产品 ID
 * @property string $category_id 分类 ID
 * @property string $name 名称
 * @property string $preview 主图
 * @property int $created_at 创建时间
 * @property int $updated_at 修改时间
 * @property int $status 状态
 *
 * @property array galleries 组图数据
 *
 * @property \admin\models\Categories $category
 * @property \admin\models\ProductGalleries[] $gallery
 */
class Products extends \common\models\Products
{

    public $galleries;

    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            ['category_id', 'default', 'value' => 0],
            [['galleries'], 'safe'],
        ]);
    }

    public function beforeSave($insert)
    {
        if(!$this->veneer){
            $this->veneer = join(",", array_keys(Yii::$app->params['veneer']['铜饰面']));
        }

        if(!$this->outside_handle){
            $this->outside_handle = join(",", array_keys(Yii::$app->params['outside_handle']));
        }

        if(!$this->inside_handle){
            $this->inside_handle = join(",", array_keys(Yii::$app->params['inside_handle']));
        }

        if(!$this->lockset){
            $this->lockset = join(",", array_keys(Yii::$app->params['lockset']));
        }

        return parent::beforeSave($insert);
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);

        if(isset($changedAttributes['preview']) && $changedAttributes['preview'] != $this->preview){
            File::delFile($changedAttributes['preview'], true);
        }

        if($this->gallery){
            foreach($this->gallery as $gallery){
                if(!in_array($gallery->file, $this->galleries['image'])){
                    $gallery->delete();
                }
            }
        }

        ProductGalleries::deleteAll(['product_id' => $this->id]);

        if (isset($this->galleries['image']) && $this->galleries['image']) {
            foreach($this->galleries['image'] as $gallery){
                (new ProductGalleries(['product_id' => $this->id, 'file' => $gallery]))->save();
            }
        }

        Folder::clearBuffer(Yii::$app->user->id);
    }

    public function getCategory()
    {
        return $this->hasOne(Categories::className(), ['id' => 'category_id']);
    }

    public function getCategoryName()
    {
        return $this->category ? $this->category->name : '';
    }

    public function getGallery()
    {
        return $this->hasMany(ProductGalleries::className(), ['product_id' => 'id']);
    }

    public function getStatus()
    {
        $datas = $this->getStatusSelectData();

        return isset($datas[$this->status]) ? $datas[$this->status] : '';
    }

    public function getStatusLabel()
    {
        if($this->status == self::STATUS_ACTIVE){
            $class = 'label-primary';
        }else{
            $class = 'label-danger';
        }

        return Utils::label($this->getStatus(), $class);
    }

    public function getStatusSelectData()
    {
        return [
            self::STATUS_UNACTIVE => '禁用',
            self::STATUS_ACTIVE => '启用',
        ];
    }
}
